/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package e.odbo.data.impexp.model;

import com.openbravo.data.loader.serialize.serializer.SerializerRead;
import com.openbravo.data.loader.serialize.serializer.SerializerWrite;
import com.openbravo.data.loader.sentence.SentenceExec;
import com.openbravo.data.loader.sentence.SentenceFind;
import com.openbravo.data.loader.sentence.SentenceList;
import e.odbo.data.dao.table.I_TableSentenceBuilder;
import e.odbo.data.dao.table.TableDefinition;
import e.odbo.data.dao.table.TableSentenceBuilderWarp;
import e.odbo.data.impexp.FieldConverse;
import e.odbo.data.impexp.FieldGenerator;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public abstract class AbstractImpExpTableDefinition {
    protected TableDefinition baseTable;
    int[] filterIndexs;
    Map<Integer, FieldConverse> filedConversions;//用与定义对应字段的转换类
    Map<Integer, FieldGenerator> fieldGenerators;//对应字段的值发生器，只对插入有效。

    static int STATE_Default = 0;
    static int STATE_Imported = 1;
    static int STATE_Exported = 2;
    int state;
    List<ImpExpRowDefinition> datas;

    TableSentenceBuilderWarp tableSentenceBuilderWarp;

    public AbstractImpExpTableDefinition(TableDefinition baseTable, I_TableSentenceBuilder tableSentenceBuilder) {
        this.baseTable = baseTable;

        filedConversions = new HashMap<Integer, FieldConverse>();
        fieldGenerators = new HashMap<Integer, FieldGenerator>();
        filterIndexs = new int[]{};
        datas = new ArrayList<ImpExpRowDefinition>();
        this.tableSentenceBuilderWarp = new TableSentenceBuilderWarp(baseTable, tableSentenceBuilder);
    }

    public ImpExpRowDefinition getRow(int rowIndex) {
        return datas.get(rowIndex);
    }

    public abstract Object[] getData(int rowIndex);

    public abstract int[] getFieldIndexs();

    public abstract int[] getUpdateFieldIndexs();


    public SerializerRead getSerializerReadBasic() {
        return baseTable.getSerializerReadBasic(getListFieldIndexs());
    }

    public SerializerWrite getSerializerInsertBasic() {
        return baseTable.getSerializerInsertBasic(getListFieldIndexs());
    }

    public SerializerWrite getSerializerDeleteBasic() {
        return baseTable.getSerializerDeleteBasic();
    }

    public SerializerWrite getSerializerUpdateBasic() {
        //return baseTable.getSerializerUpdateBasic(getUpdateFieldIndexs());
        return baseTable.getSerializerUpdateBasic(getListFieldIndexs());
    }

    public SentenceExec getInsertSentence() {
        return getInsertSentence(getListFieldIndexs());
    }

    public SentenceExec getInsertSentence(int[] fieldindx) {
        return tableSentenceBuilderWarp.getInsertSentence(fieldindx);
        //return new PreparedSentence(s, getInsertSQL(), getSerializerInsertBasic(), null);
    }

    public SentenceList getListSentence() {
        return getListSentence(getSerializerReadBasic());
    }

    public SentenceList getListSentence(SerializerRead sr) {
        return tableSentenceBuilderWarp.getListSentence(sr);
        //return new PreparedSentence(s, getListSQL(), null, sr);
    }

    public SentenceExec getDeleteSentence() {
        return getDeleteSentence(getSerializerDeleteBasic());
    }

    public SentenceExec getDeleteSentence(SerializerWrite sw) {
        return tableSentenceBuilderWarp.getDeleteSentence(sw);
        //return new PreparedSentence(s, getDeleteSQL(), sw, null);
    }

    public SentenceExec getUpdateSentence() {
        //return getUpdateSentence(getUpdateFieldIndexs());
        return getUpdateSentence(getListFieldIndexs());
    }

    public SentenceExec getUpdateSentence(int[] fieldindx) {
        return tableSentenceBuilderWarp.getUpdateSentence(fieldindx);
        //return new PreparedSentence(s, getUpdateSQL(), getSerializerUpdateBasic(), null);
    }

    /**
     * public String getListSQL() {
     * return baseTable.getListSQL(getListFieldIndexs());
     * }
     * <p>
     * public String getListSQL(Date start, Date end) {
     * String sql = getListSQL();
     * String dateField = baseTable.getDateFieldName();
     * if (dateField != null) {
     * sql += " where ";
     * if (start != null && end != null) {
     * sql += baseTable.getDateFieldName() + ">=" + DataWriteUtils.getSQLValue(start);
     * sql += " and " + baseTable.getDateFieldName() + "<=" + DataWriteUtils.getSQLValue(end);
     * } else if (start != null) {
     * sql += baseTable.getDateFieldName() + ">=" + DataWriteUtils.getSQLValue(start);
     * } else if (end != null) {
     * sql += baseTable.getDateFieldName() + "<=" + DataWriteUtils.getSQLValue(end);
     * } else
     * sql += "(1=1)";
     * }
     * return sql;
     * }
     * <p>
     * <p>
     * <p>
     * <p>
     * public String getDeleteSQL() {
     * return baseTable.getDeleteSQL();
     * }
     * <p>
     * public String getInsertSQL() {
     * return baseTable.getInsertSQL(getListFieldIndexs());
     * }
     * <p>
     * public String getUpdateSQL() {
     * // return baseTable.getUpdateSQL(getUpdateFieldIndexs());
     * return baseTable.getUpdateWhoutIgnoreSQL(getListFieldIndexs());
     * }
     **/

    public int[] getListFieldIndexs() {
        return getFieldIndexs().length == 0 ? baseTable.getAllFields() : getFieldIndexs();
    }

    public TableDefinition getBaseTable() {
        return baseTable;
    }

    public int getRowCount() {
        return datas.size();
    }

    public Map<Integer, FieldGenerator> getFieldGenerators() {
        return fieldGenerators;
    }

    public Map<Integer, FieldConverse> getFiledConversions() {
        return filedConversions;
    }

    public int[] getFilterIndexs() {
        return filterIndexs;
    }

    public int getState() {
        return state;
    }


    public void addRow(ImpExpRowDefinition row) {
        datas.add(row);
    }

    public SentenceFind getExistSentence() {
        return tableSentenceBuilderWarp.getExistSentence(baseTable.getSerializerExistBasic());
        //return baseTable.getExistSentence(baseTable.getSerializerExistBasic());
    }

    public SentenceList getListWithTimeSentence(SerializerRead sr, Date start, Date end) {
        return tableSentenceBuilderWarp.getListWithTimeSentence(sr, start, end);
    }

    public I_TableSentenceBuilder getTableSentenceBuilder() {
        return tableSentenceBuilderWarp.getBuilder();
    }
}
